<?
require_once("functions.php");

/*******************************************************
Function:	enableSurvivalPicks($ID, $Week)
Updated:	10/30/2007
Description:	Changes NHLUsers.Survivor from OUT to IN if the deadline week
				has not passed.
********************************************************/
function enableSurvivalPicks($ID, $Week)
{
	mysql_query("SELECT PickEmailed FROM NHLSchedule WHERE Week=".$Week." AND PickEmailed=1");
	if(mysql_affected_rows() > 0)
	{
		return "Expired";
	}
	else
	{
		mysql_query("UPDATE NHLUsers SET Survivor='IN' WHERE ID=$ID") or die("Cannot query the database.<br/>");
		return "IN";
	}
}

/*******************************************************
Function:	SurvivorPickDropDown($ID,$Week)
Updated:	10/25/2007
Description:	Provides a drop down of teams playing for the week, and removes previous
				Survivor picks.
********************************************************/
function SurvivorPickDropDown($ID,$Week)
{
	// See if the user has made a survivor pick for this week
	$SQL = "SELECT Pick FROM NHLSurvivalPicks WHERE UserID=$ID AND Week=$Week";
	$result = mysql_query($SQL) or die("Cannot query the database.<br/>");
	if($myrow = mysql_fetch_array($result))	//Loop through list of Users, make Quick Picks for each
		$Pick = $myrow["Pick"];
		
	
	if ($Pick)
	{
		// See if Game picked has started, if so they can't change pick
		$SQL = "SELECT PickEmailed, Winner FROM NHLSchedule WHERE Week=$Week AND (Home='$Pick' OR Away='$Pick')";
		$result = mysql_query($SQL) or die("Cannot query the database.<br/>");
		if($myrow = mysql_fetch_array($result))	
		{
			$CanPick = $myrow["PickEmailed"];
			$Winner = $myrow["Winner"];
		}
		if ($CanPick == 1)
		{
			if ($Winner == $Pick)
				echo "<span style=\"color: #FF0000; font-weight: bold;\">$Pick</span>";
			else
				echo "<span style=\"color: #0000FF; font-weight: bold;\">$Pick</span>";
			return;
		}
	}
	
	// Get all teams from NHLSchedule table for week = $Week
	$SQL = "SELECT Home, Away FROM NHLSchedule WHERE Week=$Week";
	$result = mysql_query($SQL) or die("Cannot query the database.<br/>");
	$num = 0;
	while($myrow = mysql_fetch_array($result))	//Loop through list of games, add both Home & Away teams to list
	{
		$ThisWeeksTeams[$num] = $myrow["Away"];
		$num++;
		$ThisWeeksTeams[$num] = $myrow["Home"];
		$num++;
	}
	
	// Get all teams user has picked in Survivor Pool
	$SQL = "SELECT Pick FROM NHLSurvivalPicks WHERE UserID=$ID AND Week < $Week";
	$result = mysql_query($SQL) or die("Cannot query the database.<br/>");
	$num = 0;
	while($myrow = mysql_fetch_array($result))	//Loop through list of Users, make Quick Picks for each
	{
		if ($myrow["Pick"] != "")
		{
			$PrevPicks[$num] = $myrow["Pick"];
			$num++;
		}
	}
		
	// Narrow list to teams STILL available this week.
	if ($PrevPicks)
		$choices = array_diff($ThisWeeksTeams, $PrevPicks);
	else
		$choices = $ThisWeeksTeams;
	
	//Display the DropDown
	if(!$Pick)
		echo "<select name=\"Survivor\" class=\"select\" style=\"background-color: #FFFF99;\"><option selected value=\"\">Make Pick</option>\n";
	else
		echo "<select name=\"Survivor\" class=\"select\">\n";
		
	// Loop through each (still available) choice
	foreach ($choices as $value)
	{
		if ($value == $Pick)
			echo "<option selected value=\"$value\">$value</option>\n";
		else
			echo "<option value=\"$value\">$value</option>\n";
	}
	echo "</select>\n";		
}

/*******************************************************
Function:	IsSurviving($ID)
Updated:	10/30/2007
Description:	Returns true if user is in the survivor pool.
********************************************************/
function IsSurviving($ID)
{
	mysql_query("SELECT Survivor FROM NHLUsers WHERE ID=$ID AND Survivor='IN'");
	if(mysql_affected_rows() > 0)
		return true;
	else
		return false;
}



/*******************************************************
Function:	teamsAvailable($ID,$Week)
Updated:	10/31/2007
Description:	Returns array of available teams for the week.
********************************************************/
function teamsAvailable($ID,$Week)
{
	// Get all teams from NHLSchedule table for week = $Week
	$SQL = "SELECT Home, Away FROM NHLSchedule WHERE Week=$Week";
	$result = mysql_query($SQL) or die("Cannot query the database.<br/>");
	$num = 0;
	while($myrow = mysql_fetch_array($result))	//Loop through list of games, add both Home & Away teams to list
	{
		$ThisWeeksTeams[$num] = $myrow["Away"];
		$num++;
		$ThisWeeksTeams[$num] = $myrow["Home"];
		$num++;
	}
	
	// Get all teams user has picked in Survivor Pool
	$SQL = "SELECT Pick FROM NHLSurvivalPicks WHERE UserID=$ID AND Week < $Week";
	$result = mysql_query($SQL) or die("Cannot query the database.<br/>");
	$num = 0;
	while($myrow = mysql_fetch_array($result))	//Loop through list of Picks
	{
		if ($myrow["Pick"] != "")
		{
			$PrevPicks[$num] = $myrow["Pick"];
			$num++;
		}
	}
		
	// Narrow list to teams STILL available this week.
	if ($PrevPicks)
		$choices = array_diff($ThisWeeksTeams, $PrevPicks);
	else
		$choices = $ThisWeeksTeams;
	
	return $choices;
}

/*******************************************************
Function:	DisplayPicked($ID)
Updated:	10/31/2007
Description:	Displays all survivor picks the user has made.
********************************************************/
function DisplayPicked($ID)
{
	$ThisWeek = getWeek();
	echo "<h4>Previous Picks By Week</h4>
		<table cellspacing=\"0\" cellpadding=\"3\" border=\"1\" style=\"border-collapse: collapse\">
			<tr>";
	
	// Get all teams user has picked in Survivor Pool
	$SQL = "SELECT Pick, Week FROM NHLSurvivalPicks WHERE (UserID=$ID AND Week<$ThisWeek) ORDER BY Week ASC";
	$result = mysql_query($SQL) or die("Cannot query the database.<br/>");
	
	// Display the Week Row
	for ($i = ($ThisWeek-mysql_affected_rows()); $i < $ThisWeek; $i++)
		echo "<td width=\"40\" align=\"center\">$i</td>";
	echo "  </tr>
			<tr>";
	// Display the picks row
	while($myrow = mysql_fetch_array($result))	//Loop through list of Picks
		if ($myrow["Pick"] != "")
			echo "<td width=\"40\" align=\"center\"><img src=\"../images/small/".$myrow["Pick"].".gif\" title=\"".$myrow["Pick"]."\"/></td>";
	echo "</tr>
		</table>";
}

/*******************************************************
Function:	DisplayAvailable($ID,$ThisWeek)
Updated:	10/31/2007
Description:	Displays available for the user to make to survive.
********************************************************/
function DisplayAvailable($ID,$ThisWeek)
{
	$Teams = teamsAvailable($ID,$ThisWeek);
	echo "<h4>Available For Week $ThisWeek</h4>
		<table cellspacing=\"0\" cellpadding=\"3\" border=\"1\" style=\"border-collapse: collapse\">\n";
	$num = 0;
	foreach ($Teams as $Available)
	{
		if (($num % 10) == 0)
			echo "<tr>\n";
		echo "<td width=\"40\" align=\"center\"><img src=\"../images/small/$Available.gif\" title=\"$Available\" onClick=\"goSet(PicksForm.Survivor,'$Available')\"></td>\n";
		if (($num % 10) == 9)
			echo "</tr>\n";
		$num++;
	}
	for ($i = ($num%10); $i < 10; $i++)
		echo "<td width=\"40\" align=\"center\">&nbsp;</td>\n";
	echo "</tr></table>";
	/**/
}

?>